<?php
namespace AuctionCore\Common;


/**
* redis操作类
*@author爱民
*/
class PhpRedis
{
    private $redis = null;

    public function __construct($dbindex=0){
        $redis = new \Redis();
        if($this->redis == null){
            $res = $redis->connect(C('REDIS_HOST'), C('REDIS_PORT'), 1 );
            $this->redis = $redis;
        }
        $this->select($dbindex);
    }

    public function ping()
    {
       return $this->redis->ping();
    }

    public function select($num){
        return $this->redis->select($num);
    }

    public function set($key,$vale,$expire=false){

         $ok = $this->redis->set($key,$vale);

        if($expire){
          $this->redis->expire($key,$expire);    
        }
        return $ok;
    }

    public function get($key){
        return $this->redis->get($key);
    }

    public function del($key){
        return $this->redis->del($key);
    }

    public function getRedis(){
        return $this->redis;
    }

    public function lock($key){        
        return $this->redis->setNx($key,1)?true:false;
    }
    public function lock_wait($key){
        $r = $this->lock($key);
        if(!$r){
            for ($i=0; $i <5 ; $i++) { 
                usleep(rand(500,1000));
                $r = $this->lock($key);
                if($r)
                   break;
               $i++;
            }
        }
        return $r;
    }

    public function unlock($key){
        $this->redis->delete($key);
    }

    public function incr($key){
        $this->redis->incr($key);
    }

    public function rPush($key,$value)
    {
        return $this->redis->rPush($key,$value);
    }
    public function exists($key)
    {
        return $this->redis->exists($key);
    }

    /**列表取操作
     * @param $key
     * @return string
     */
    public function lPop($key){
         return $this->redis->lPop($key);
    }
    /**
     * 获取hash类型缓存
     * @param $key
     * @param $hkey
     * @return string
     */
    public function hget($key,$hkey){
        return $this->redis->hGet($key,$hkey);
    }

    /***
     *  设置hash类型缓存
     * @param $key
     * @param $hkey
     * @param $val
     * @return int
     */
    public function hset($key,$hkey,$val){
        return $this->redis->hSet($key,$hkey,$val);
    }

    /**
     * 删除hash类型缓存
     * @param $key
     * @param $hkey
     * @return int
     */
    public function hdel($key,$hkey){
        return $this->redis->hDel($key,$hkey);
    }
    //添加有序集合
    public function zadd($key,$score,$member){
        return $this->redis->zadd($key,$score,$member);
    }
    //取出有序集合中的元素
    public function zrange($key,$star,$end,$withscores=false){
        return $this->redis->zRange($key,$star,$end,$withscores?'withscores':'');
    }


    //移除有序集合一个或多个成员
    public function zrem($key,$member){
        return $this->redis->zrem($key,$member);
    }

    //添加集合
    public function sadd($key,$value){
        return $this->redis->sadd($key,$value);
    }
    //获取匹配的key
    public function keys($key){
        return $this->redis->keys($key);
    }
    //取出有序集合中的元素
    public function zscore($key,$member){
        return $this->redis->zscore($key,$member);
    }



}
